<template>
  <div class="bookmark-card" @mouseenter="hover = true" @mouseleave="hover = false">
    <div class="bookmark-content" :class="{ 'is-hover': hover }">
      <!-- 网站图标和标题区域 -->
      <div class="bookmark-header">
        <div class="favicon-wrapper">
          <img 
            v-if="bookmark.favicon" 
            :src="bookmark.favicon" 
            :alt="bookmark.title"
            @error="handleFaviconError"
          >
          <i v-else class="el-icon-link"></i>
        </div>
        <h3 class="bookmark-title" :title="bookmark.title">{{ bookmark.title }}</h3>
      </div>

      <!-- 描述区域 -->
      <p v-if="bookmark.description" class="bookmark-description" :title="bookmark.description">
        {{ bookmark.description }}
      </p>

      <!-- URL和分类信息 -->
      <div class="bookmark-meta">
        <span class="bookmark-url" :title="bookmark.url">{{ formatUrl(bookmark.url) }}</span>
        <span v-if="category" class="bookmark-category">
          <i :class="category.icon || 'el-icon-folder'" :style="{ color: category.color }"></i>
          {{ category.name }}
        </span>
      </div>

      <!-- 悬浮时显示的操作按钮 -->
      <div class="bookmark-actions" v-show="hover">
        <el-tooltip content="在新标签页中打开" placement="top" :open-delay="500">
          <el-button
            type="text"
            icon="el-icon-link"
            @click.stop="openUrl(bookmark.url)"
          ></el-button>
        </el-tooltip>
        <el-tooltip content="复制链接" placement="top" :open-delay="500">
          <el-button
            type="text"
            icon="el-icon-document-copy"
            @click.stop="copyUrl(bookmark.url)"
          ></el-button>
        </el-tooltip>
      </div>

      <!-- 点击次数和最后点击时间 -->
      <div class="bookmark-stats" v-if="bookmark.clickCount > 0">
        <el-tooltip :content="getLastClickedText(bookmark.lastClickedAt)" placement="top">
          <span class="click-count">
            <i class="el-icon-view"></i>
            {{ bookmark.clickCount }}
          </span>
        </el-tooltip>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'BookmarkCard',
  props: {
    bookmark: {
      type: Object,
      required: true
    },
    category: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      hover: false,
      showDefaultFavicon: false
    }
  },
  methods: {
    formatUrl(url) {
      try {
        const urlObj = new URL(url)
        return urlObj.hostname
      } catch {
        return url
      }
    },
    handleFaviconError() {
      this.showDefaultFavicon = true
    },
    openUrl(url) {
      window.open(url, '_blank')
      this.$emit('click', this.bookmark)
    },
    async copyUrl(url) {
      try {
        await navigator.clipboard.writeText(url)
        this.$message.success('链接已复制到剪贴板')
      } catch (err) {
        this.$message.error('复制失败')
      }
    },
    getLastClickedText(lastClickedAt) {
      if (!lastClickedAt) return ''
      const date = new Date(lastClickedAt)
      const now = new Date()
      const diff = now - date
      
      // 转换为易读的时间格式
      if (diff < 60000) { // 小于1分钟
        return '刚刚访问'
      } else if (diff < 3600000) { // 小于1小时
        const minutes = Math.floor(diff / 60000)
        return `${minutes}分钟前访问`
      } else if (diff < 86400000) { // 小于1天
        const hours = Math.floor(diff / 3600000)
        return `${hours}小时前访问`
      } else if (diff < 2592000000) { // 小于30天
        const days = Math.floor(diff / 86400000)
        return `${days}天前访问`
      } else {
        return `最后访问: ${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
      }
    }
  }
}
</script>

<style lang="less" scoped>
.bookmark-card {
  margin-bottom: 20px;
  
  .bookmark-content {
    position: relative;
    padding: 16px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
    transition: all 0.3s ease;
    cursor: pointer;
    
    &:hover, &.is-hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
    }
    
    .bookmark-header {
      display: flex;
      align-items: center;
      margin-bottom: 8px;
      
      .favicon-wrapper {
        width: 24px;
        height: 24px;
        margin-right: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        
        img {
          width: 16px;
          height: 16px;
          object-fit: contain;
        }
        
        i {
          font-size: 16px;
          color: #909399;
        }
      }
      
      .bookmark-title {
        margin: 0;
        font-size: 16px;
        font-weight: 500;
        color: #303133;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        flex: 1;
      }
    }
    
    .bookmark-description {
      margin: 8px 0;
      font-size: 13px;
      color: #606266;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      line-height: 1.5;
      min-height: 20px;
    }
    
    .bookmark-meta {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-top: 12px;
      font-size: 13px;
      
      .bookmark-url {
        color: #909399;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        max-width: 60%;
      }
      
      .bookmark-category {
        display: flex;
        align-items: center;
        color: #606266;
        
        i {
          margin-right: 4px;
          font-size: 14px;
        }
      }
    }
    
    .bookmark-actions {
      position: absolute;
      top: 12px;
      right: 12px;
      display: flex;
      gap: 8px;
      opacity: 0;
      transition: opacity 0.2s ease;
      background: rgba(255, 255, 255, 0.9);
      padding: 4px;
      border-radius: 4px;
      
      .el-button {
        padding: 4px;
        font-size: 16px;
        
        &:hover {
          color: #409EFF;
        }
      }
    }
    
    .bookmark-stats {
      position: absolute;
      bottom: 12px;
      right: 12px;
      font-size: 12px;
      color: #909399;
      
      .click-count {
        display: flex;
        align-items: center;
        gap: 4px;
        
        i {
          font-size: 14px;
        }
      }
    }
    
    &:hover {
      .bookmark-actions {
        opacity: 1;
      }
    }
  }
}

// 响应式设计
@media screen and (max-width: 768px) {
  .bookmark-card {
    .bookmark-content {
      .bookmark-meta {
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
        
        .bookmark-url {
          max-width: 100%;
        }
      }
      
      .bookmark-actions {
        position: static;
        opacity: 1;
        margin-top: 12px;
        justify-content: flex-end;
      }
    }
  }
}
</style> 